/*
 * Copyright (c) 2001 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 *    rfu - Added setting for background update interval.
 */
package net.sourceforge.p4eclipse.ui.prefs;


import net.sourceforge.p4eclipse.core.Policy;
import net.sourceforge.p4eclipse.ui.PerforceUIPlugin;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

/**
  * @version 	1.0
  * @author		<a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class PerforcePreferencePage extends PreferencePage implements IWorkbenchPreferencePage 
{
	private Text portText;
	private Text clientText;
	private Text userText;
	private Button showDirty;
	private Button deleteAffectsChangeList;
	private Button performBackgroundUpdates;	
	private Text updateInterval;
	private Text inactivityTimeout;
	
	/**
	 * The constructor.
	 */
	public PerforcePreferencePage() 
	{
	}

	/**
	 * Insert the method's description here.
	 * @see PreferencePage#init
	 */
	public void init(IWorkbench workbench)  
	{
		setPreferenceStore(PerforceUIPlugin.getPlugin().getPreferenceStore());
	}

	/**
	 * Insert the method's description here.
	 * @see PreferencePage#createContents
	 */
	protected Control createContents(Composite parent)  
	{
		Policy.assertNotNull(parent);
		
		Composite composite = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		composite.setLayout(layout);
		composite.setLayoutData(new GridData(GridData.FILL_BOTH));
		
		createLabel(composite, "Port:");
		portText = createText(composite);
		
		createLabel(composite, "Client:");
		clientText = createText(composite);
		
		createLabel(composite, "User:");				
		userText = createText(composite);
		
		performBackgroundUpdates =
			createCheckBox(composite, "Perform Background Cache Updates");
		performBackgroundUpdates.addSelectionListener(new SelectionAdapter() 
		{
			/** @see org.eclipse.swt.events.SelectionListener#widgetSelected(SelectionEvent) */
			public void widgetSelected(SelectionEvent e) 
			{
				boolean b = performBackgroundUpdates.getSelection();
				updateInterval.setEnabled(b);
			}
		});

		createLabel(composite, "\tBackground Update Interval (sec.):");
		updateInterval = createText(composite);

		createLabel(composite, "Inactivity Timeout (sec.):");
		inactivityTimeout = createText(composite);

		showDirty = createCheckBox(composite, "&Compute deep outgoing state for folders (disabling this will improve decorator performance)"); 

		deleteAffectsChangeList = createCheckBox(composite, "&Deleting resources will remove them in the repository (p4 delete)"); 

		initializeValues();
	
		return composite;
	}
	
	private void initializeValues()
	{
		portText.setText(getPreferenceStore().getString(PerforceUIPlugin.P4PORT_PREFERENCE));
		clientText.setText(getPreferenceStore().getString(PerforceUIPlugin.P4CLIENT_PREFERENCE));
		userText.setText(getPreferenceStore().getString(PerforceUIPlugin.P4USER_PREFERENCE));

		performBackgroundUpdates.setSelection(getPreferenceStore().getBoolean(PerforceUIPlugin.CACHE_UPD_ENABLED));

		showDirty.setSelection(getPreferenceStore().getBoolean(PerforceUIPlugin.CALCULATE_DIRTY_PREFERENCE));
		deleteAffectsChangeList.setSelection(getPreferenceStore().getBoolean(PerforceUIPlugin.DELETE_AFFECTS_CHANGELIST));
		
		long currentUpdateInterval =
			getPreferenceStore().getLong(PerforceUIPlugin.CACHE_UPD_INTERVAL_PREFERENCE);
		updateInterval.setText(Long.toString(currentUpdateInterval));
		updateInterval.setEnabled(performBackgroundUpdates.getSelection());
		
		long inactivityTimeoutValue =
			getPreferenceStore().getLong(PerforceUIPlugin.INACTIVITY_TIMEOUT);
		inactivityTimeout.setText(Long.toString(inactivityTimeoutValue));
		
	}

	protected static Label createLabel(Composite parent, String labelText)
	{
		Policy.assertNotNull(parent);
		Policy.assertNotNull(labelText);		
		
		Label result = new Label(parent, SWT.WRAP);
		result.setText(labelText);
		result.setLayoutData(new GridData());
		
		return result;
	}	

	protected static Text createText(Composite parent)
	{
		Policy.assertNotNull(parent);
		
		Text result = new Text(parent, SWT.BORDER);
		GridData data = new GridData(GridData.FILL_HORIZONTAL);
		result.setLayoutData(data);
		
		return result;
	}

	protected static Button createCheckBox(Composite group, String label) 
	{
		Button result = new Button(group, SWT.CHECK);
		result.setText(label);

		GridData data = new GridData(GridData.FILL_HORIZONTAL);
		data.horizontalSpan = 2;
		result.setLayoutData(data);
	
		return result;
	}

	
	/*
	 * @see IPreferencePage#performOk()
	 */
	public boolean performOk()
	{
		long updateIntervalValue = 120;
		long inactivityTimeoutValue = 15;
		try
		{
			updateIntervalValue = Long.parseLong(updateInterval.getText());
			inactivityTimeoutValue = Long.parseLong(inactivityTimeout.getText());
			
			getPreferenceStore().setValue(PerforceUIPlugin.CACHE_UPD_INTERVAL_PREFERENCE, updateIntervalValue);
			getPreferenceStore().setValue(PerforceUIPlugin.INACTIVITY_TIMEOUT, inactivityTimeoutValue);
		}
		catch (NumberFormatException e)
		{
			return false;
		}						
		
		getPreferenceStore().setValue(PerforceUIPlugin.CACHE_UPD_ENABLED, performBackgroundUpdates.getSelection());
		getPreferenceStore().setValue(PerforceUIPlugin.P4PORT_PREFERENCE, portText.getText());
		getPreferenceStore().setValue(PerforceUIPlugin.P4CLIENT_PREFERENCE, clientText.getText());
		getPreferenceStore().setValue(PerforceUIPlugin.P4USER_PREFERENCE, userText.getText());
		getPreferenceStore().setValue(PerforceUIPlugin.CALCULATE_DIRTY_PREFERENCE, showDirty.getSelection());
		getPreferenceStore().setValue(PerforceUIPlugin.DELETE_AFFECTS_CHANGELIST, deleteAffectsChangeList.getSelection());
				
		PerforceUIPlugin.getPlugin().resetClient();
		
		return true;
	}

	/**
	 * @see PreferencePage#performDefaults()
	 */
	protected void performDefaults()
	{
		portText.setText(getPreferenceStore().getDefaultString(PerforceUIPlugin.P4PORT_PREFERENCE));
		clientText.setText(getPreferenceStore().getDefaultString(PerforceUIPlugin.P4CLIENT_PREFERENCE));
		userText.setText(getPreferenceStore().getDefaultString(PerforceUIPlugin.P4USER_PREFERENCE));

		showDirty.setSelection(getPreferenceStore().getDefaultBoolean(PerforceUIPlugin.CALCULATE_DIRTY_PREFERENCE));
		deleteAffectsChangeList.setSelection(getPreferenceStore().getDefaultBoolean(PerforceUIPlugin.DELETE_AFFECTS_CHANGELIST));
		performBackgroundUpdates.setSelection(getPreferenceStore().getDefaultBoolean(PerforceUIPlugin.CACHE_UPD_ENABLED));
		updateInterval.setText("120");
		updateInterval.setEnabled(performBackgroundUpdates.getSelection());
		
		inactivityTimeout.setText("15");

	}
}
